﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;

namespace WindowsFormMultiLang
{
    public class LanguageManager
    {
        public static string LangName = "";

        /// <summary>
        /// 改变窗体的语言。
        /// </summary>
        /// <param name="language"></param>
        /// <param name="form"></param>
        public static void Change(string language, Form form)
        {
            try
            {
                LangName = language;
                //读出xml语言文件
                var doc = new XmlDocument();
                var file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "i18n", language);
                doc.Load(file);

                //将资源加载到字典中
                var dict = new Dictionary<string, string>();
                foreach (XmlNode node in doc.SelectSingleNode("//resources").ChildNodes)
                {
                    dict.Add(node.Name, node.InnerText);
                }

                //找窗体上的LanguageProvider控件
                var providerField = form.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic).FirstOrDefault(s => s.FieldType == typeof(LanguageProvider));
                if (providerField != null)
                {
                    var provider = (LanguageProvider)providerField.GetValue(form);
                    ApplyResource(provider, new ResourceDictionary(dict), form);
                }
            }
            catch (Exception)
            {
               // MessageBox.Show("i18n File Not Exist");
            }
        }

        /// <summary>
        /// 应用控件的资源文本。
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="dict"></param>
        /// <param name="component"></param>
        private static void ApplyResource(LanguageProvider provider, ResourceDictionary dict, IComponent component)
        {
            ResourceResolverManager.GetResolver(component.GetType())?.Apply(provider, component, dict);
        }
    }

    /// <summary>
    /// 资源字典。
    /// </summary>
    public class ResourceDictionary
    {
        private Dictionary<string, string> dict = null;

        public ResourceDictionary(Dictionary<string, string> dict)
        {
            this.dict = dict;
        }

        /// <summary>
        /// 尝试通过 <paramref name="key"/> 找到资源，如果找到的话，调用 <paramref name="action"/> 应用语言。
        /// </summary>
        /// <param name="key"></param>
        /// <param name="action"></param>
        public void TryGet(string key, Action<string> action)
        {
            var value = string.Empty;
            if (dict.TryGetValue(key, out value))
            {
                action(value);
            }
        }
    }

    /// <summary>
    /// 控件资源的解析器
    /// </summary>
    public interface IResourceResolver
    {
        /// <summary>
        /// 对组件应用语言。
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="component"></param>
        /// <param name="dict"></param>
        void Apply(LanguageProvider provider, IComponent component, ResourceDictionary dict);
    }

    /// <summary>
    /// 资源解析管理器。
    /// </summary>
    public class ResourceResolverManager
    {
        private static Dictionary<Type, IResourceResolver> resolvers = new Dictionary<Type, IResourceResolver>();

        static ResourceResolverManager()
        {
            //注册各种类型的解析器
            resolvers.Add(typeof(DataGridView), new DataGridResolver());
            resolvers.Add(typeof(ToolStrip), new ToolStripResolver());
            resolvers.Add(typeof(ToolStripItem), new ToolStripItemResolver());
            resolvers.Add(typeof(ListView), new ListViewResolver());
            resolvers.Add(typeof(Control), new ControlResolver());
        }

        /// <summary>
        /// 获取类型对应的解析器。
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static IResourceResolver GetResolver(Type type)
        {
            //查找此种类型的解析器
            var resolver = resolvers.FirstOrDefault(s => s.Key.IsAssignableFrom(type));
            if (resolver.Value != null)
            {
                return resolver.Value;
            }

            return null;
        }
    }

    /// <summary>
    /// 解析器基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class ResourceResolverBase<T> : IResourceResolver where T : IComponent
    {
        public abstract void Apply(LanguageProvider provider, T component, ResourceDictionary dict);

        /// <summary>
        /// 对组件应用语言。
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="component"></param>
        /// <param name="dict"></param>
        public void Apply(LanguageProvider provider, IComponent component, ResourceDictionary dict)
        {
            Apply(provider, (T)component, dict);
        }
    }

    /// <summary>
    /// Control 的解析器。
    /// </summary>
    public class ControlResolver : ResourceResolverBase<Control>
    {
        public override void Apply(LanguageProvider provider, Control component, ResourceDictionary dict)
        {
            var resourceName = provider.GetResourceName(component);
            dict.TryGet(resourceName, value => component.Text = value);

            //弹出菜单
            if (component.ContextMenuStrip != null)
            {
                ResourceResolverManager.GetResolver(component.ContextMenuStrip.GetType())?.Apply(provider, component.ContextMenuStrip, dict);
            }

            //查找子控件，递归应用语言
            foreach (IComponent child in (component as Control).Controls)
            {
                ResourceResolverManager.GetResolver(child.GetType())?.Apply(provider, child, dict);
            }
        }
    }

    /// <summary>
    /// ToolStrip 解析器。包括 StatusStrip、MenuStrip等等。
    /// </summary>
    public class ToolStripResolver : ResourceResolverBase<ToolStrip>
    {
        public override void Apply(LanguageProvider provider, ToolStrip component, ResourceDictionary dict)
        {
            var resourceName = provider.GetResourceName(component);
            dict.TryGet(resourceName, value => component.Text = value);

            foreach (IComponent child in component.Items)
            {
                ResourceResolverManager.GetResolver(child.GetType())?.Apply(provider, child, dict);
            }
        }
    }

    /// <summary>
    /// DataGrid解析器
    /// </summary>
    public class DataGridResolver : ResourceResolverBase<DataGridView>
    {
        public override void Apply(LanguageProvider provider, DataGridView component, ResourceDictionary dict)
        {
            foreach (DataGridViewColumn column in component.Columns)
            {
                var resourceName = provider.GetResourceName(column);
                if (!string.IsNullOrEmpty(resourceName))
                {
                    dict.TryGet(resourceName, value => column.HeaderText = value);
                }
            }
        }
    }

    /// <summary>
    /// ToolStripItem 解析器。
    /// </summary>
    public class ToolStripItemResolver : ResourceResolverBase<ToolStripItem>
    {
        public override void Apply(LanguageProvider provider, ToolStripItem component, ResourceDictionary dict)
        {
            var resourceName = provider.GetResourceName(component);
            if (!string.IsNullOrEmpty(resourceName))
            {
                dict.TryGet(resourceName, value => component.Text = value);
            }

            //如果是下拉菜单
            if (component is ToolStripMenuItem)
            {
                foreach (IComponent child in (component as ToolStripMenuItem).DropDownItems)
                {
                    ResourceResolverManager.GetResolver(child.GetType())?.Apply(provider, child, dict);
                }
            }
        }
    }

    /// <summary>
    /// ListView 解析器。
    /// </summary>
    public class ListViewResolver : ResourceResolverBase<ListView>
    {
        public override void Apply(LanguageProvider provider, ListView component, ResourceDictionary dict)
        {
            foreach (ColumnHeader column in component.Columns)
            {
                var resourceName = provider.GetResourceName(column);
                if (!string.IsNullOrEmpty(resourceName))
                {
                    dict.TryGet(resourceName, value => column.Text = value);
                }
            }
        }
    }
}
